app.controller('SaleShiftTplCtrl', function ($scope, ajax, $modal, toaster ,$stateParams,$filter,$timeout,$rootScope) {
	
    $scope.salesId=$stateParams.saleId;
    
    $scope.subject=$stateParams.subject;
	
    $scope.reload = $scope.page.loader('sales/bs/salesShiftTpl/{salesId}/{pageIndex}/{pageSize}/page'.format({salesId: $scope.salesId}));
    $scope.$on('pageIndex', function(event, p) { $scope.reload(p, $scope.queryMap); });
    $scope.reload(1);
    
    
    //获取线路列表
    $scope.loadRouteList = function () {
        var promise = ajax.call(new Req.GET({
            resource: 'route/bs/list'
        }));
        promise.then(function (result) {
            $scope.routeList = result.result;
        }, function () { });
    };
    $scope.loadRouteList();
	
	//模糊查询
	$scope.queryMap = {};
	$scope.query = function () {
		if ($scope.routeList.selected != undefined) {
			$scope.queryMap.template__route__id = $scope.routeList.selected.id;
		} else if ($scope.routeList.selected == undefined) {
			delete $scope.queryMap.template__route__id;
		}	
        $scope.reload(1, $scope.queryMap);     
	};
	
	
	var inserter = new Req.POST({ resource: $scope.resource });

    // 添加
	$scope.add = function () {
		var modalInstance = $modal.open({
            templateUrl: 'template/bus/salesShiftTemplate/writeModal.html?v=ec99ae1516',
            controller: 'BusSalesShiftTplAddCtrl',
            size: 'lg',
            resolve: {
                param: function () {
                    return {
                        "salesId": $scope.salesId
                    };
                }
            }
        });
		modalInstance.result.then(function (param) {
			inserter.datalist = param;
			inserter.resource = 'sales/bs/'+$scope.salesId+'/salesShiftTpl';
			ajax.call(inserter).then(function (result) {
				if (result.code == 200) {
					toaster.pop('success', '成功', '关联班次档案成功。');
					$scope.reload(1);
				} else {
					toaster.pop('error', '错误', result.result);
				}
			}, function () {
				toaster.pop('error', '错误', '新增信息失败。');
			});
		});
    };
    
	var deleter = new Req.DELETE({ resource: $scope.resource });
    
	//删除
	$scope.del = function (size, current_data, submit_type) {
		var modalInstance = $scope.delModal(size, current_data, submit_type);
		modalInstance.result.then(function (param) {
			if (param.type == 'delete') {
				deleter.id = param.data.id;
				deleter.resource = 'sales/bs/' + deleter.id + '/delete';
				$scope.page.del(deleter, $scope.reload);
			}
		});
	};

	
});
;



